
//init state
const state = () => ({
  param: [
/*    {
      comId: 1,
      buyNum: 2,
      serviceId: 1
    },*/
    /*{
      comId: 2,
      buyNum: 2,
      serviceId: 2
    }*/
  ]
})

//getters
const getters = {
  getData(state, getters, rootState, rootGetters) {
    return state.param.map(item => {
      let comMsg = rootGetters["DetailData/getComImgAndComPrice"](item.comId);
      item = {...item, ...comMsg};
      let serviceName = rootGetters["ServiceMsg/getSerNameBySerId"](item.serviceId);
      item = {...item, ...{serviceName: serviceName}};
      return item;
    })
  },
  getBuyNum(state){
    let num = 0;
    state.param.filter(item => {
      num +=item.buyNum;
      if (item.serviceId) ++num;
    })
    return num;
  },
  getSubmitTotalPrice(state,getter){
    let dataMsg = getter["getData"];
    return dataMsg.reduce((pre,next) => {
      let price = pre + (next.buyNum*next.comPrice);
      let servicePrice = 0;
      if (next.serviceId) {
        servicePrice = next.serviceName.split("￥")[1];
      }
      return price + parseInt(servicePrice);
    },0)
  },
  refreshOrderSubmitDataSessionStorageData(state){//刷新session中的方法
    sessionStorage.setItem("OrderSubmitData",JSON.stringify(state.param))
  }
}

//actions
const actions = {
  loadDataForOrderSubmitData({commit},payload){
    commit("loadDataForOrderSubmitDataMut",payload)
  }
}

//mutations
const mutations = {
  loadDataForOrderSubmitDataMut(state,val){
    state.param = [...val];
  }
}

export default {
  namespaced: true,
  state,
  getters,
  actions,
  mutations
}
